﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class Groups : MonoBehaviour {

	private List<Waves> waves;
	public float minX = -50;
	public bool canMove;
	public float scale = 1f;
	public int sortOrder;
	public float moveSpeed = 0.00f;
	public GameObject target;
	public float InvalideValue = -9999f;
	void Start () {
		if (gameObject.name == "Bg") {
			minX = -20;
		} else {
			minX = -50;
		}
		if (gameObject.name == "Bg") {
			if(PlayerPrefs.GetInt("select") == 1){
				if (PlayerPrefs.GetInt ("hasBg") == 1) {
					moveSpeed = PlayerPrefs.GetFloat ("bgIF");
				}
			}
		} else {
			if(PlayerPrefs.GetInt("select") == 1){
				if (PlayerPrefs.GetInt ("hasForward") == 1) {
					moveSpeed = PlayerPrefs.GetFloat ("forwardIF");
				}
			}
		}
		int count = transform.GetChildCount ();
		waves = new List<Waves> ();
		for (var i = 0; i < count; i++) {
			waves.Add(transform.GetChild (i).GetComponent<Waves>());
			waves [i].Init (scale);
			if (i == 0) {
				waves [i].Reset (0f, false, 0, 0, 0, 0, 0);
			} else {
				var preWave = waves [i - 1];
				if (preWave.m_remain > 0) {
					waves [i].Reset (preWave.m_lastY, true, preWave.m_lastA, preWave.m_lastB, preWave.m_remain, preWave.m_lastAngle, preWave.m_lastSingleCount);
				} else {
					waves [i].Reset (preWave.m_lastY, false, 0, 0, 0, 0, 0);
				}
			}
		}
		for (var i = 0; i < count; i++) {
			if (i == 0) {
				waves [i].transform.position = new Vector3 (minX+transform.position.x, transform.position.y, 0);
			} else {
				waves [i].transform.position = new Vector3 (waves[i-1].transform.position.x + (waves[i-1].WaveLength-waves[i].m_xOff)*scale + transform.position.x, transform.position.y, 0);
			}
			waves[i].GetComponent<Renderer> ().sortingOrder = sortOrder;
		}
		if (target != null) {
			target.transform.position = new Vector3 (1.3f, getY (1.3f)+transform.position.y, 0);
		}
	}

	public float getY(float x){
		int length = waves.Count;
		for (var i = 0; i < length; i++) {
			Waves wave = waves [i];
			if (wave.transform.position.x <= x && wave.transform.position.x + (wave.WaveLength - wave.m_xOff)*scale >= x) {
				for(var k=0; k<wave.dataList.Count; k++){
					var data = wave.dataList [k];
					float startX = (data.baseIndex * wave.m_xOff)*scale + wave.transform.position.x;
					float endX = ((data.baseIndex + data.remian - 1) * wave.m_xOff)*scale + wave.transform.position.x;
					if (startX <= x && endX >= x) {
						float oX = x - startX;
						float f = 0;
						if (data.isHalf) {
							float oo = data.lastSingleCount - data.remian;
							f = oX / wave.m_xOff + oo;
						} else {
							f = oX / wave.m_xOff;
						}
						return data.a * Mathf.Sin (Mathf.Deg2Rad * (f/(float)(data.lastSingleCount-1)*180 + data.angle)) - data.oY + transform.position.y;
					}
				}
			}
		}
		return InvalideValue;
	}

	public Vector2 getXY(float x){
		int length = waves.Count;
		for (var i = 0; i < length; i++) {
			Waves wave = waves [i];
			if (wave.transform.position.x <= x && wave.transform.position.x + (wave.WaveLength - wave.m_xOff)*scale > x) {
				for(var k=0; k<wave.dataList.Count; k++){
					var data = wave.dataList [k];
					float startX = (data.baseIndex * wave.m_xOff)*scale + wave.transform.position.x;
					float endX = ((data.baseIndex + data.remian - 1) * wave.m_xOff)*scale + wave.transform.position.x;
					if (startX <= x && endX >= x) {
						float oX = x - startX;
						float f = 0;
						if (data.isHalf) {
							float oo = data.lastSingleCount - data.remian;
							f = oX / wave.m_xOff + oo;
						} else {
							f = oX / wave.m_xOff;
						}
						float baseY = Mathf.Sin (Mathf.Deg2Rad * (f/(float)(data.lastSingleCount-1)*180 + data.angle));
						float baseX = Mathf.Cos (Mathf.Deg2Rad * (f / (float)(data.lastSingleCount - 1) * 180 + data.angle));
						return new Vector2 (baseX, baseY);
					}
				}
			}
		}
		return Vector2.zero;
	}

	void Update () {
		int length = waves.Count;
		if (canMove) {
			for (var i = 0; i < length; i++) {
				Waves wave = waves [i];
				wave.transform.position = new Vector3 (wave.transform.position.x - moveSpeed*scale*Time.deltaTime, wave.transform.position.y, wave.transform.position.z);
			}
		}
		CameraMoved cameraMove = Camera.main.GetComponent<CameraMoved> ();
		float x = waves [0].transform.position.x + (waves [0].WaveLength - waves [0].m_xOff) * scale;
		if (cameraMove.OutLeft (x)) {
			Waves wave = waves [0];
			var lastWavePos = waves [length-1].transform.position;
			wave.transform.position = new Vector3 (lastWavePos.x + (wave.WaveLength-wave.m_xOff)*scale, lastWavePos.y, lastWavePos.z);
			var preWave = waves [length-1];
			if (preWave.m_remain > 0) {
				waves [0].Reset (preWave.m_lastY, true, preWave.m_lastA, preWave.m_lastB, preWave.m_remain, preWave.m_lastAngle, preWave.m_lastSingleCount);
			} else {
				waves [0].Reset (preWave.m_lastY, false, 0, 0, 0, 0, 0);
			}
			waves.Remove (wave);
			waves.Add (wave);
		}
	}

}
